package cn.iocoder.yudao.module.ems.service.warehousing;

import cn.iocoder.yudao.module.ems.controller.admin.warehousing.vo.WarehousingInfoBaseVO;
import cn.iocoder.yudao.module.ems.controller.admin.warehousing.vo.WarehousingInfoCreateReqVO;
import cn.iocoder.yudao.module.ems.controller.admin.warehousing.vo.WarehousingInfoRespVO;
import cn.iocoder.yudao.module.ems.controller.admin.warehousing.vo.WarehousingUpdateReqVO;
import cn.iocoder.yudao.module.ems.convert.warehousing.WarehousingConvert;
import cn.iocoder.yudao.module.ems.convert.warehousing.WarehousingInfoConvert;
import cn.iocoder.yudao.module.ems.dal.dataobject.spares.SparesDO;
import cn.iocoder.yudao.module.ems.dal.dataobject.warehousing.WarehousingDO;
import cn.iocoder.yudao.module.ems.dal.dataobject.warehousing.WarehousingInfoDO;
import cn.iocoder.yudao.module.ems.dal.mysql.spares.SparesMapper;
import cn.iocoder.yudao.module.ems.dal.mysql.warehousing.WarehousingInfoMapper;
import cn.iocoder.yudao.module.ems.service.spares.SparesServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ems.enums.ErrorCodeConstants.SPARES_ITEMNO_NOTEXISTS;
import static cn.iocoder.yudao.module.ems.enums.ErrorCodeConstants.WAREHOUSING_NOT_EXISTS;

/**
 * 入库信息 Service 实现类
 *
 * @author dason
 */
@Service
@Validated
public class WarehousingInfoServiceImpl implements WarehousingInfoService {

    @Resource
    private WarehousingInfoMapper warehousingInfoMapper;

    @Resource
    private SparesMapper sparesMapper;

    private void validateWarehousingInfoExists(Long id) {
        if (warehousingInfoMapper.selectById(id) == null) {
            throw exception(WAREHOUSING_NOT_EXISTS);
        }
    }

    @Override
    public List<WarehousingInfoDO> getWarehousingInfoList(Long warehousingId) {
        return warehousingInfoMapper.selectList(warehousingId);
    }

    @Override
    public void deleteWarehousingInfo(Long id) {
        // 校验存在
        this.validateWarehousingInfoExists(id);
        // 删除
        warehousingInfoMapper.deleteById(id);
    }

    @Override
    public void insertBatchInfo(Collection<WarehousingInfoCreateReqVO> infoList) {
        // 校验物料编号存在
        for(WarehousingInfoCreateReqVO info: infoList) {
            SparesDO spares = sparesMapper.selectByItemNo(info.getItemNo());
            if (spares == null) {
                throw exception(SPARES_ITEMNO_NOTEXISTS);
            }
        }
        // 插入
        List<WarehousingInfoDO> list = WarehousingInfoConvert.INSTANCE.convert2(infoList);
        this.warehousingInfoMapper.insertBatch(list);
    }


    @Override
    public void updateWarehousingInfo(WarehousingInfoRespVO updateReqVO) {
        // 校验存在
        this.validateWarehousingInfoExists(updateReqVO.getId());
        // 更新
        WarehousingInfoDO updateObj = WarehousingInfoConvert.INSTANCE.convert3(updateReqVO);
        warehousingInfoMapper.updateById(updateObj);
    }

}
